import React, { Component } from 'react';
import {
	Platform,
	StyleSheet,
	Text,
	View,
	Image,
	ToastAndroid,
	Alert,
	FlatList,
	AppRegistry,
	BackAndroid
} from 'react-native';

import pxToDp from '../../../utils/pxToDp'

export default class Apply extends Component {
	constructor(props) {
		super(props)
		this.state = {
			data: [{
				"approvalCode": "111845",
				"businesskey": "PQIACG7X7W8B6EL0KWB8EAWITEL3CAHT",
				"createTime": 1512377022000,
				"createUser": 8,
				"createUserNickname": "业务1",
				"hotels": [{
					"actExecutionId": null,
					"bookUser": null,
					"businessTripId": 47,
					"createTime": 1512377022000,
					"createUser": 8,
					"depDate": 1513872000000,
					"hotelState": 0,
					"id": 40,
					"inDate": 1514476800000,
					"orderNumber": null,
					"stopCity": "上海",
					"updateTime": null
				}],
				"id": 47,
				"logs": null,
				"routes": [{
					"actExecutionId": null,
					"arrCity": "上海",
					"bookUser": null,
					"businessTripId": 47,
					"createTime": 1512377022000,
					"createUser": 8,
					"deptCity": "北京",
					"deptDate": 1512921600000,
					"id": 58,
					"orderNumber": null,
					"routeState": 0,
					"routeType": 1,
					"updateTime": null
				}],
				"tripContent": "开会\n",
				"tripDepartLocation": "北京",
				"tripEndDate": 1514476800000,
				"tripLocation": "上海",
				"tripPersons": "李数",
				"tripStartDate": 1512316800000,
				"tripState": 1,
				"updateTime": null
			}, {
				"approvalCode": "110649",
				"businesskey": "4EHYZQ4IESRZ6R9A9HGX5DUIL7ROVP88",
				"createTime": 1512376921000,
				"createUser": 8,
				"createUserNickname": "业务1",
				"hotels": [{
					"actExecutionId": null,
					"bookUser": null,
					"businessTripId": 46,
					"createTime": 1512376921000,
					"createUser": 8,
					"depDate": 1513872000000,
					"hotelState": 0,
					"id": 39,
					"inDate": 1514476800000,
					"orderNumber": null,
					"stopCity": "上海",
					"updateTime": null
				}],
				"id": 46,
				"logs": null,
				"routes": [{
					"actExecutionId": null,
					"arrCity": "上海",
					"bookUser": null,
					"businessTripId": 46,
					"createTime": 1512376921000,
					"createUser": 8,
					"deptCity": "北京",
					"deptDate": 1512921600000,
					"id": 57,
					"orderNumber": null,
					"routeState": 0,
					"routeType": 1,
					"updateTime": null
				}],
				"tripContent": "开会\n",
				"tripDepartLocation": "北京",
				"tripEndDate": 1514476800000,
				"tripLocation": "上海",
				"tripPersons": "李数",
				"tripStartDate": 1512316800000,
				"tripState": 1,
				"updateTime": null
			}, {
				"approvalCode": "119103",
				"businesskey": "CUUD9MAQO68C4H9ZLVXZ2BWK2SFV5Y0K",
				"createTime": 1512376856000,
				"createUser": 8,
				"createUserNickname": "业务1",
				"hotels": [{
					"actExecutionId": null,
					"bookUser": null,
					"businessTripId": 45,
					"createTime": 1512376856000,
					"createUser": 8,
					"depDate": 1510675200000,
					"hotelState": 0,
					"id": 38,
					"inDate": 1511280000000,
					"orderNumber": null,
					"stopCity": "上海",
					"updateTime": null
				}],
				"id": 45,
				"logs": null,
				"routes": [],
				"tripContent": "开会",
				"tripDepartLocation": "北京",
				"tripEndDate": 1511280000000,
				"tripLocation": "上海",
				"tripPersons": "HH",
				"tripStartDate": 1509552000000,
				"tripState": 1,
				"updateTime": null
			}, {
				"approvalCode": "116722",
				"businesskey": "G4PVIR9HHWCHYMAVD9VNAZIIAS35ZH03",
				"createTime": 1512376743000,
				"createUser": 8,
				"createUserNickname": "业务1",
				"hotels": [{
					"actExecutionId": null,
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"depDate": 1513872000000,
					"hotelState": 0,
					"id": 37,
					"inDate": 1514476800000,
					"orderNumber": null,
					"stopCity": "上海",
					"updateTime": null
				}],
				"id": 44,
				"logs": null,
				"routes": [{
					"actExecutionId": null,
					"arrCity": "上海",
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"deptCity": "北京",
					"deptDate": 1513180800000,
					"id": 56,
					"orderNumber": null,
					"routeState": 0,
					"routeType": 1,
					"updateTime": null
				}],
				"tripContent": "开会 ",
				"tripDepartLocation": "北京",
				"tripEndDate": 1514476800000,
				"tripLocation": "上海",
				"tripPersons": "李数",
				"tripStartDate": 1513180800000,
				"tripState": 1,
				"updateTime": null
			}, {
				"approvalCode": "116722",
				"businesskey": "G4PVIR9HHWCHYMAVD9VNAZIIAS35ZH03",
				"createTime": 1512376743000,
				"createUser": 8,
				"createUserNickname": "业务1",
				"hotels": [{
					"actExecutionId": null,
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"depDate": 1513872000000,
					"hotelState": 0,
					"id": 37,
					"inDate": 1514476800000,
					"orderNumber": null,
					"stopCity": "上海",
					"updateTime": null
				}],
				"id": 44,
				"logs": null,
				"routes": [{
					"actExecutionId": null,
					"arrCity": "上海",
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"deptCity": "北京",
					"deptDate": 1513180800000,
					"id": 56,
					"orderNumber": null,
					"routeState": 0,
					"routeType": 1,
					"updateTime": null
				}],
				"tripContent": "开会 ",
				"tripDepartLocation": "北京",
				"tripEndDate": 1514476800000,
				"tripLocation": "上海",
				"tripPersons": "李数",
				"tripStartDate": 1513180800000,
				"tripState": 1,
				"updateTime": null
			}, {
				"approvalCode": "116722",
				"businesskey": "G4PVIR9HHWCHYMAVD9VNAZIIAS35ZH03",
				"createTime": 1512376743000,
				"createUser": 8,
				"createUserNickname": "业务1",
				"hotels": [{
					"actExecutionId": null,
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"depDate": 1513872000000,
					"hotelState": 0,
					"id": 37,
					"inDate": 1514476800000,
					"orderNumber": null,
					"stopCity": "上海",
					"updateTime": null
				}],
				"id": 44,
				"logs": null,
				"routes": [{
					"actExecutionId": null,
					"arrCity": "上海",
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"deptCity": "北京",
					"deptDate": 1513180800000,
					"id": 56,
					"orderNumber": null,
					"routeState": 0,
					"routeType": 1,
					"updateTime": null
				}],
				"tripContent": "开会 ",
				"tripDepartLocation": "北京",
				"tripEndDate": 1514476800000,
				"tripLocation": "上海",
				"tripPersons": "李数",
				"tripStartDate": 1513180800000,
				"tripState": 1,
				"updateTime": null
			}, {
				"approvalCode": "116722",
				"businesskey": "G4PVIR9HHWCHYMAVD9VNAZIIAS35ZH03",
				"createTime": 1512376743000,
				"createUser": 8,
				"createUserNickname": "业务1",
				"hotels": [{
					"actExecutionId": null,
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"depDate": 1513872000000,
					"hotelState": 0,
					"id": 37,
					"inDate": 1514476800000,
					"orderNumber": null,
					"stopCity": "上海",
					"updateTime": null
				}],
				"id": 44,
				"logs": null,
				"routes": [{
					"actExecutionId": null,
					"arrCity": "上海",
					"bookUser": null,
					"businessTripId": 44,
					"createTime": 1512376743000,
					"createUser": 8,
					"deptCity": "北京",
					"deptDate": 1513180800000,
					"id": 56,
					"orderNumber": null,
					"routeState": 0,
					"routeType": 1,
					"updateTime": null
				}],
				"tripContent": "开会 ",
				"tripDepartLocation": "北京",
				"tripEndDate": 1514476800000,
				"tripLocation": "上海",
				"tripPersons": "李数",
				"tripStartDate": 1513180800000,
				"tripState": 1,
				"updateTime": null
			}],
			status:false,
		};				
	}
	onBackAndroid = () => {
		if(this.state.status){
			this.props.showHide(true)
			this.setState({
				status:false,
			})
			return true;//不退出
		}		
		return false;
	}
	componentWillMount(){
	  if (Platform.OS === 'android') {
	    BackAndroid.addEventListener('hardwareBackPress', this.onBackAndroid);
	  } 
	}
	componentWillUnmount(){
	  if (Platform.OS === 'android') {
	    BackAndroid.removeEventListener('hardwareBackPress', this.onBackAndroid);
	  }
	}
	
	scroll(e){	
		this.props.showHide(false)
		this.setState({
			status:true,
		})
		/*let y = e.nativeEvent.contentOffset.y;
		if(y>this.state.site){
			//Alert.alert('温馨提醒',`正在向下滚`)
			this.props.showHide(false)
		}else{
			//Alert.alert('温馨提醒',`正在向上滚`)
			this.props.showHide(true)
		}
		this.setState({
			site:y
		})			*/								
	}
	
	_keyExtractor = (item, index) => item.id;
	render() {
		return(
			<View style={styles.container}>
				<FlatList
				  horizontal={false}
		          data={this.state.data}
		          keyExtractor={this._keyExtractor}
		          ref="_flatlist"
		          onScroll={this.scroll.bind(this)}
		          renderItem={({item}) => 
		          	<View style={styles.main}>
						<View style={styles.top}>
							<Text>{item.businesskey}</Text>
						</View>
						<View style={styles.bottom}>
							<Text>{this.state.site}</Text>	
						</View>	
					</View>		
		          }
		        />				
			</View>
		)
	}
}

const styles = StyleSheet.create({
	container: {
		backgroundColor: '#f4f5f7',
		paddingLeft: pxToDp(20),
		paddingRight: pxToDp(20),
		paddingBottom: pxToDp(20),
		flex:1,
	},
	main: {
		borderWidth: 1,
		borderColor: '#f2f3f5',
		backgroundColor: 'white',
		marginTop: pxToDp(20),
		paddingRight: pxToDp(20),
		paddingLeft: pxToDp(20),
	},
	top: {
		height: pxToDp(88),
		borderBottomWidth: 1,
		borderBottomColor: '#f2f3f5',
	},
	bottom: {
		height: pxToDp(130),
	}

});


